package codeformat;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class SetOffDutyTimeAction extends AnAction {
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        OffDutyTimeConfig config = OffDutyTimeConfig.getInstance();
        LocalTime currentTime = config.getOffDutyTime();
        
        String input = Messages.showInputDialog(
                e.getProject(),
                "请输入下班时间 (格式 HH:mm):",
                "设置下班时间",
                Messages.getQuestionIcon(),
                currentTime.format(TIME_FORMATTER),
                null
        );
        
        if (input != null && !input.trim().isEmpty()) {
            try {
                LocalTime newTime = LocalTime.parse(input.trim(), TIME_FORMATTER);
                config.setOffDutyTime(newTime);
                // 显示成功消息
                Messages.showInfoMessage(
                        e.getProject(),
                        "下班时间已设置为: " + newTime.format(TIME_FORMATTER),
                        "设置成功"
                );
            } catch (DateTimeParseException ex) {
                Messages.showErrorDialog(
                        e.getProject(),
                        "时间格式不正确，请使用 HH:mm 格式",
                        "设置失败"
                );
            }
        }
    }
}